﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Red.Controls.Diagrams.Declarators;
using Red.Controls.Diagrams.ViewModels;
using Red.Core;

namespace Red.Toolkit.NodeDeclaratorEditor
{
    public class NodeDeclaratorEditorViewModel : ViewModel
    {
        #region Data

        /// <summary>
        /// Declarator that is currently being edited.
        /// </summary>
        private NodeDeclarator _selectedDeclarator;

        /// <summary>
        /// List of outputs of currently displayed node.
        /// This list is accessible via stacker.
        /// </summary>
        private DummyOutputsListDataItem _outputs;

        /// <summary>
        /// List of drops of currently displayed node.
        /// This list is accessible via stacker.
        /// </summary>
        private DummyDropsListDataItem _drops;

        /// <summary>
        /// The currently selected node.
        /// </summary>
        private DummyNodeViewModel _selectedNode;

        #endregion Data

        #region Properties

        public ObservableCollection<NodeDeclarator> AllDeclarators { get; set; }

        public NodeDeclarator SelectedDeclarator
        {
            get { return _selectedDeclarator; }
            set
            {
                _selectedDeclarator = value;
                OnPropertyChanged( "BaseClasses" );
                OnPropertyChanged();
            }
        }

        public DummyOutputsListDataItem Outputs
        {
            get { return _outputs; }
            set
            {
                _outputs = value;
                OnPropertyChanged();
            }
        }

        public DummyDropsListDataItem Drops
        {
            get { return _drops; }
            set
            {
                _drops = value;
                OnPropertyChanged();
            }
        }

        public DummyNodeViewModel SelectedNode
        {
            get { return _selectedNode; }
            set
            {
                _selectedNode = value;

                // refresh list of outputs and drops that will be displayed in stacker
                Outputs = new DummyOutputsListDataItem( value );
                Drops = new DummyDropsListDataItem( value );

                OnPropertyChanged();
            }
        }

        public NodeDeclarators NodeDeclarators { get; set; }

        public IEnumerable<NodeDeclarator> BaseClasses
        {
            get { return AllDeclarators.Except( new List<NodeDeclarator> { SelectedDeclarator } ); }
        }

        #endregion Properties

        #region Public methods

        public void Save( string filePath )
        {
            NodeDeclarators.Save( filePath, AllDeclarators );
        }

        #endregion Public methods
    }
}
